package com.tee.live.im.core.server.common;

import com.tee.im.constants.ImConstants;

import java.io.Serial;
import java.io.Serializable;
import java.util.Arrays;

/**
 * @author tee
 * @date 2025/9/30
 */
public class ImMsg implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    // 魔数 用于做基本校验
    private short magic;

    // 用于标识当前消息作用,后续会交给不同的handler去处理
    private int code;

    // 用于记录body的长度
    private int len;

    // 存储消息体的内容,一般会按照字节数组的方式去存储
    private byte[] body;

    public static ImMsg build(int code,String data) {

        ImMsg imMsg = new ImMsg();
        imMsg.setMagic(ImConstants.DEFAULT_MAGIC);
        imMsg.setLen(data.getBytes().length);
        imMsg.setCode(code);
        imMsg.setBody(data.getBytes());
        return imMsg;
    }

    public short getMagic() {

        return magic;
    }

    public void setMagic(final short magic) {

        this.magic = magic;
    }

    public int getLen() {

        return len;
    }

    public void setLen(final int len) {

        this.len = len;
    }

    public int getCode() {

        return code;
    }

    public void setCode(final int code) {

        this.code = code;
    }

    public byte[] getBody() {

        return body;
    }

    public void setBody(final byte[] body) {

        this.body = body;
    }

    @Override
    public String toString() {
        return "ImMsg{" +
                "body=" + Arrays.toString(body) +
                ", magic=" + magic +
                ", code=" + code +
                ", len=" + len +
                '}';
    }
}
